//
//  SignInViewController.m
//  Kwapro
//
//  Created by Leafc on 13-12-26.
//  Copyright (c) 2013年 Leafc. All rights reserved.
//

#import "SignInViewController.h"
#import "AFNetworking.h"

@interface SignInViewController () {
    UITextField *_userName;
    UITextField *_password;
}

@end

@implementation SignInViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
	
    _userName = [[UITextField alloc] initWithFrame:CGRectMake(15, 100, 200, 50)];
    _userName.backgroundColor = [UIColor orangeColor];
    _userName.text = @"test";
    _userName.placeholder = @"用户名";
    [self.view addSubview:_userName];
    
    _password = [[UITextField alloc] initWithFrame:CGRectMake(15, 100 + 50 + 10, 200, 50)];
    _password.backgroundColor = [UIColor orangeColor];
    _password.secureTextEntry = YES;
    _password.text = @"123456";
    _password.placeholder = @"密码";
    [self.view addSubview:_password];
    
    UIButton *btnOK = [[UIButton alloc] initWithFrame:CGRectMake(15, 220, 100, 50)];
    btnOK.backgroundColor = [UIColor orangeColor];
    [btnOK setTitle:@"确定" forState:UIControlStateNormal];
    [btnOK addTarget:self action:@selector(btnOKAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnOK];
    
    UIButton *btnCancel = [[UIButton alloc] initWithFrame:CGRectMake(150, 220, 100, 50)];
    btnCancel.backgroundColor = [UIColor orangeColor];
    [btnCancel setTitle:@"取消" forState:UIControlStateNormal];
    [btnCancel addTarget:self action:@selector(btnCancelAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnCancel];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)btnOKAction:(UIButton *)sender
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager POST:@"http://58.255.32.228/KWAPRO/apiweb/login" parameters:@{@"user": _userName.text, @"pwd": _password.text} constructingBodyWithBlock:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"%@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
}

- (void)btnCancelAction:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
